package com.company.algo.listnode;

import com.company.algo.pojo.ListNode;

public class DeleteDuplicateInSorted {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head;
        ListNode newHead = new ListNode(-1);
        ListNode nxt, tmp = newHead, pre = null;
        while (cur != null) {
            nxt = cur.next;
            if (pre != null && nxt != null && cur.val != pre.val && cur.val != nxt.val) {
                tmp.next = new ListNode(cur.val);
                tmp = tmp.next;
            }else if (pre == null && nxt != null && cur.val != nxt.val) {
                tmp.next = new ListNode(cur.val);
                tmp = tmp.next;
            }else if (pre != null && nxt == null && cur.val != pre.val) {
                tmp.next = new ListNode(cur.val);
                tmp = tmp.next;
            }else if (pre == null && nxt == null){
                tmp.next = new ListNode(cur.val);
                tmp = tmp.next;
            }
            pre = cur;
            cur = cur.next;
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        DeleteDuplicateInSorted Main = new DeleteDuplicateInSorted();
        ListNode head = new ListNode(1);
//        head.next = new ListNode(2);
//        head.next.next = new ListNode(2);
//        head.next.next.next = new ListNode(3);
        ListNode newHead = Main.deleteDuplicates(head);
        while (newHead!=null){
            System.out.print(newHead.val+"->");
            newHead = newHead.next;
        }
    }
}
